<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="ruleForm.role" placeholder="请选择">
          <el-option v-for="(item, index) in roleList" :key="index" :label="item.role_key" :value="item.role_key"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input :disabled="ruleForm.id ? true : false" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="用户编号" prop="uid">
        <el-input v-model="ruleForm.uid"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="ruleForm.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户头像" prop="avatar">
        <el-upload class="avatar-uploader" :action="this.$upload" :on-error="uploadFileError" :on-success="uploadFileSuccess" :show-file-list="false">
          <img v-if="ruleForm.avatar != ''" :src="ruleForm.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="onSubmit('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRoleList } from '@/api/roleApi.js'
export default {
  data() {
    //邮箱验证
    var emailRule = (rule, value, callback) => {
      let patt = new RegExp('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$')
      if (!patt.test(value) && value != '') {
        callback(new Error('邮箱格式不对,请重新输入'))
      } else {
        callback()
      }
    }
    //手机号验证
    var phoneRule = (rule, value, cb) => {
      let patt = new RegExp('^1[3-9]\\d{9}$')
      if (!patt.test(value) && value != '') {
        cb(new Error('手机号格式不对，请重新输入'))
      } else {
        cb()
      }
    }
    return {
      roleList: [],
      rules: {
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 9, message: '长度在 3 到 9 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
        ],
        role: [{ type: 'array', required: true, message: '请选择角色', trigger: 'change' }],
        uid: [
          { required: true, message: '请输入用户编号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11，限数字', trigger: 'blur' },
        ],
        email: [{ validator: emailRule, trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { validator: phoneRule, trigger: 'blur' },
        ],
      },
    }
  },
  props: ['ruleForm'],
  created() {
    this.getRole()
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submit', this.ruleForm)
        } else {
          this.$message.warning('请按表单要求填写完整！！！')
          return false
        }
      })
    },
    getRole() {
      getRoleList().then((res) => {
        this.roleList = res.data.data
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    //图片上传成功
    uploadFileSuccess(res) {
      this.ruleForm.avatar = res.data.url
    },
    //图片上传失败
    uploadFileError(err) {
      console.log(err)
    },
  },
}
</script>

<style lang="less" scoped>
</style>